﻿using System;
using System.Web;
using System.Web.Routing;
//using Microsoft.AspNet.Identity.Owin;
//using Microsoft.Owin;
//using Microsoft.Owin.Security;

namespace MisRepresentantes.Core.Utilities
{
    public static class HttpContextFactory
    {
        [ThreadStatic]
        private static HttpContextBase _httpContext;

        //[ThreadStatic]
        //private static IOwinContext _owinContext;

        [ThreadStatic]
        private static RouteData _routeData;

        //[ThreadStatic]
        //private static ExternalLoginInfo _externalLoginInfo;

        public static void SetHttpContext(HttpContextBase context)
        {
            _httpContext = context;
        }

        //public static void SetOwinContext(IOwinContext context)
        //{
        //    _owinContext = context;
        //}

        public static void Reset()
        {
            ResetHttpContext();
            //ResetOwinContext();
        }

        public static void ResetHttpContext()
        {
            _httpContext = null;
        }

        //public static void ResetOwinContext()
        //{
        //    _owinContext = null;
        //}

        public static HttpContextBase GetHttpContext()
        {
            return _httpContext ?? new HttpContextWrapper(HttpContext.Current);
        }

        //public static IOwinContext GetOwinContext()
        //{
        //    return _owinContext ?? GetHttpContext().GetOwinContext();
        //}

        //public static Task<ExternalLoginInfo> GetExternalLoginInfoAsync()
        //{
        //    return _externalLoginInfo == null ? GetOwinContext().Authentication.GetExternalLoginInfoAsync() : new Task<ExternalLoginInfo>(new System.Func<ExternalLoginInfo>(() => { return _externalLoginInfo; }));
        //}

        public static void SetRouteData(RouteData routeData)
        {
            _routeData = routeData;
        }

        public static void ResetRouteData()
        {
            _routeData = null;
        }

        public static RouteData GetRouteData(HttpContextBase httpContext)
        {
            return _routeData ?? RouteTable.Routes.GetRouteData(httpContext);
        }
    }
}